using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WinformTemplete.Modules.Blog.Models;
using SqlSugar;
using NLog;

namespace WinformTemplete.Modules.Blog.Repositories
{
    /// <summary>
    /// 博客仓储实现
    /// </summary>
    public class BlogRepository : IBlogRepository
    {
        private readonly SqlSugarClient _db;
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

        public BlogRepository(SqlSugarClient db)
        {
            _db = db;
        }

        public async Task<List<BlogModel>> GetAllAsync() // Fully qualify Blog to avoid ambiguity
        {
            try
            {
                return await _db.Queryable<BlogModel>().ToListAsync(); // Fully qualify Blog
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "获取所有博客时发生错误");
                throw;
            }
        }

        public async Task<BlogModel> GetByIdAsync(int id) // Fully qualify Blog
        {
            try
            {
                return await _db.Queryable<BlogModel>().FirstAsync(x => x.Id == id); // Fully qualify Blog
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"获取博客ID {id} 时发生错误");
                throw;
            }
        }

        public async Task<bool> AddAsync(BlogModel blog) // Fully qualify Blog
        {
            try
            {
                return await _db.Insertable(blog).ExecuteCommandAsync() > 0;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "添加博客时发生错误");
                throw;
            }
        }

        public async Task<bool> UpdateAsync(BlogModel blog) // Fully qualify Blog
        {
            try
            {
                return await _db.Updateable(blog).ExecuteCommandAsync() > 0;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "更新博客时发生错误");
                throw;
            }
        }

        public async Task<bool> DeleteAsync(int id)
        {
            try
            {
                return await _db.Deleteable< BlogModel>().Where(x => x.Id == id).ExecuteCommandAsync() > 0; // Fully qualify Blog
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"删除博客ID {id} 时发生错误");
                throw;
            }
        }

        public async Task<List< BlogModel>> SearchByTitleAsync(string title) // Fully qualify Blog
        {
            return await _db.Queryable< BlogModel>() // Fully qualify Blog
                .Where(x => x.Title.Contains(title))
                .ToListAsync();
        }

        public async Task<List< BlogModel>> GetRecentBlogsAsync(int count) // Fully qualify Blog
        {
            return await _db.Queryable< BlogModel>() // Fully qualify Blog
                .OrderByDescending(x => x.CreatedDate)
                .Take(count)
                .ToListAsync();
        }

        public async Task<int> GetTotalCountAsync()
        {
            return await _db.Queryable< BlogModel>().CountAsync(); // Fully qualify Blog
        }

        public async Task<List< BlogModel>> GetPagedBlogsAsync(int pageIndex, int pageSize) // Fully qualify Blog
        {
            return await _db.Queryable< BlogModel>() // Fully qualify Blog
                .OrderByDescending(x => x.CreatedDate)
                .ToPageListAsync(pageIndex, pageSize);
        }

        public async Task<List< BlogModel>> GetBlogsByDateRangeAsync(DateTime startDate, DateTime endDate) // Fully qualify Blog
        {
            return await _db.Queryable< BlogModel>() // Fully qualify Blog
                .Where(x => x.CreatedDate >= startDate && x.CreatedDate <= endDate)
                .OrderByDescending(x => x.CreatedDate)
                .ToListAsync();
        }
    }
} 